$(document).ready(function () {
    jQuery.extend({
        highlight: function (node, re, nodeName, className) {
            if (node.nodeType === 3) {
                var match = node.data.match(re);
                if (match) {
                    var highlight = document.createElement(nodeName || 'span');
                    highlight.className = className || 'highlight';
                    var wordNode = node.splitText(match.index);
                    wordNode.splitText(match[0].length);
                    var wordClone = wordNode.cloneNode(true);
                    highlight.appendChild(wordClone);
                    wordNode.parentNode.replaceChild(highlight, wordNode);
                    return 1; //skip added node in parent
                }
            } else if ((node.nodeType === 1 && node.childNodes) && // only element nodes that have children
                !/(script|style)/i.test(node.tagName) && // ignore script and style nodes
                !(node.tagName === nodeName.toUpperCase() && node.className === className)) { // skip if already highlighted
                for (var i = 0; i < node.childNodes.length; i++) {
                    i += jQuery.highlight(node.childNodes[i], re, nodeName, className);
                }
            }
            return 0;
        }
    });

    jQuery.fn.unhighlight = function (options) {
        var settings = { className: 'highlight', element: 'span' };
        jQuery.extend(settings, options);

        return this.find(settings.element + "." + settings.className).each(function () {
            var parent = this.parentNode;
            parent.replaceChild(this.firstChild, this);
            parent.normalize();
        }).end();
    };

    jQuery.fn.highlight = function (words, options) {
        var settings = { className: 'highlight', element: 'span', caseSensitive: false, wordsOnly: false };
        jQuery.extend(settings, options);

        if (words.constructor === String) {
            words = [words];
        }
        words = jQuery.grep(words, function(word, i){
            return word != '';
        });
        words = jQuery.map(words, function(word, i) {
            return word.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
        });
        if (words.length == 0) { return this; };

        var flag = settings.caseSensitive ? "" : "i";
        var pattern = "(" + words.join("|") + ")";
        if (settings.wordsOnly) {
            pattern = "\\b" + pattern + "\\b";
        }
        var re = new RegExp(pattern, flag);

        return this.each(function () {
            jQuery.highlight(this, re, settings.element, settings.className);
        });
    };

    $.get('testdata.json').done(function (data) {
        var table = $('#tb').DataTable({
            data: data,
            language: {
                url:'js/datatable/zh_CN.json'
            },
            columns: colConfig,
            columnDefs: colDe,
            searchHighlight: true,
        });


        //高亮无效
        table.on( 'draw', function () {
            //获得需要高亮的容器部分
            var body = $( table.table().body() );
            //如果之前有高亮把高亮部分去掉
            body.unhighlight();
            //根据搜索框里的关键字 高亮显示
            body.highlight( table.search() );
        } );
        //
        //
        // var filteredData = table
        //     .columns(10)
        //     .data()
        //     .flatten()
        //     .filter( function ( value ) {
        //         console.log('filter')
        //         if (value == 'zs') {
        //             console.log('yes');
        //             return '<span class="label-danger">'+value+'</span>';
        //         }
        //     } );
        //
        // console.log(filteredData);
        //
        $('#search').on( 'keyup', function () {
            console.log(this.value)
            table.search( this.value ).draw();
        } );





    })



    var colConfig = [{data: 'id', "sClass": "hid"},
        {data: 'id'},
        {data: 'imgUrl'},
        {data: 'title',},
        {data: 'originaldate',"searchable": false},
        {data: 'confidence'},
        {data: 'type', width: 'auto',"searchable": false},
        {data: 'state'},
        {data: 'aname',width: 'auto',"searchable": false},
        {data: 'atime'},
        {data: 'pname',width: 'auto',"searchable": false},
        {data: 'ptime'},
        {data: 'state', "sClass": "hid"}];


    var colDe = [
        {
            "data": "id",
            "render": function (data) {
                return  '<input type="checkbox" name="'+data+'" class="checkAll checkCol" />'
            },
            targets: [1]
        },
        {
            "data": 'state',
            "render": function (data, type, full) {
                // console.log(data);
                if (data == '0') {
                    return '<button class="btn btn-default">审核</button>'
                }
                else if (data == '1')
                    return '<button class="btn btn-warning">发布</button>'
                else return '<button class="btn btn-success">查看</button>'
            },
            targets: [7]
        },
        {
            "data": 'imgUrl',
            "render": function (data, type, full) {
                return "<img src=" + data + ">"
            },
            targets: [2]
        },
        // {
        //     "data": 'atime',
        //     "render": function (data, type, full) {
        //         return myUtil.formatDateTime(data)
        //     },
        //     targets: [9]
        //
        // },
        // {
        //     "data": 'ptime',
        //     "render": function (data, type, full) {
        //         // console.log(data)
        //         return myUtil.formatDateTime(data)
        //     },
        //     targets: [11]
        // },
        // {
        //     "data": 'originaldate',
        //     "render": function (data, type, full) {
        //         // console.log(data)
        //         return myUtil.formatDateTime(data)
        //     },
        //     targets: [4]
        // },
        { "orderable": false, "targets": [0, 1, 2, 3, 6, 7] }

    ];
})